var util = require('../../utils/util.js')
//index.js
//获取应用实例
var app = getApp()
Page({
  data: {
    motto: 'Hello World',
    userInfo: {},
    list:{
    }
  },
  //事件处理函数
  bindViewTap: function() {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },
































































































































  
  onLoad: function () {
    var session_id = wx.getStorageSync('session_id');
    this.getRedPacketInfo(session_id);
  },
  //获取用户的红包信息
  getRedPacketInfo: function (session_id) {
    var that = this;
    wx.request({
      url: app.data.servser + '/index/userRedpacket',
      data: {
        session_id: session_id
      },
      success: function (res) {
        var redpacket_info = res.data.user_redpacket_list;
        var list = new Array();
        if (redpacket_info.length) {
          for (var i = 0; i < redpacket_info.length; i++) {
            list[i] = new Array();
            list[i][0] = redpacket_info[i]['price'];
            list[i][1] = redpacket_info[i]['name'];
            list[i][2] = redpacket_info[i]['rule_description'];
            list[i][3] = redpacket_info[i]['use_time'] == 0 ? '未使用' : '使用时间:' + redpacket_info[i]['use_time'];
            list[i][4] = redpacket_info[i]['status'] == 0 ? 'use' : 'used';
            list[i][5] = redpacket_info[i]['status'] == 0 ? 'red_1' : 'red_2';
            list[i][6] = redpacket_info[i]['id'];
            list[i][7] = redpacket_info[i]['status'];
          }
          that.setData({ 'list': list })
        }
      }
    })
  },

  goRechargePage:function(){
     wx.navigateTo({ url: '/pages/recharge/recharge' })
  },
  
  useRedPacket:function(e){
     var that = this;
     var list = this.data.list;
     var id   = e.currentTarget.id;
     var redpacket_id = e.currentTarget.dataset.id;//获取红包的id

     list[id][4] = 'using';
     
     this.setData({'list': list })
     
     wx.showModal({
        title: '是否使用红包',
        content: '您确定使用红包吗？',
        success: function (res) {
          if (res.confirm) {
            that.requestUseRedPacket(redpacket_id,id);

          } else if (res.cancel) {
            list[id][4] = 'use';
            list[id][7] = 0;

            that.setData({
              'list': list
            })
          }
        },

     })
  },
  
  requestUseRedPacket:function(redpacket_id,id){
    var that = this;
    var list = this.data.list;
   
    wx.request({
      url: app.data.servser + '/index/useRedPacket',
      data: {
        session_id   : wx.getStorageSync('session_id'),
        id : redpacket_id
      },
      success: function (res) {
        if (res.data.status >= 0) {
          list[id][4] = 'used';
          list[id][5] = 'red_2';
          list[id][7] = 1;
          that.setData({ 'list': list });
          wx.setStorage({
            key: 'money',
            data: res.data.money
          });
        }else{
          list[id][4] = 'use';
          list[id][5] = 'red_1';
          list[id][7] = 0;
          that.setData({
            'list': list
          })
        }
      }
    })
  }

})
